package com.hiwan.system.web.menu;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hiwan.platform.utils.StringUtils;
import com.hiwan.system.dao.sequence.SeqDao;
import com.hiwan.system.pojo.MenuInfo;
import com.hiwan.system.service.menu.MenuService;

@Controller
@RequestMapping("/menu")
public class MenuController {

	protected Logger logger = LoggerFactory.getLogger(MenuController.class);

	@Autowired
	private MenuService menuService;

	/**
	 * 跳转到查询页面
	 * @return
	 */
	@RequestMapping("toQuery.do")
	public String toQuery(ModelMap model) {
		return "menu/query";
	}

	/**
	 * 列表页面
	 * @return
	 * @throws SQLException 
	 */
	@RequestMapping("list")
	public String list(ModelMap model, String parentId) {
		List<MenuInfo> menuList = menuService.getMenuInfoList(parentId);
		model.put("menuList", menuList);
		model.put("parentId", parentId);
		if (StringUtils.isBlank(parentId)) {
			return "menu/list";
		} else {
			return "menu/subList";
		}

	}


	/**
	 * 跳转到详情页面
	 * @param model
	 * @return
	 */
	@RequestMapping("toDetail")
	public String toDetail(ModelMap model, String flag, String pk) {
		MenuInfo menu = null;
		String parentMenuName =null;
		if(StringUtils.equals("add", flag)){
			menu = new MenuInfo();
			menu.setMenuId("菜单编号无需人工输入，系统自动生成");
			if(StringUtils.isBlank(pk)){
				menu.setIsLeaf("0");//父菜单
				menu.setParentMenu("");
			}else{
				menu.setIsLeaf("1");//子菜单
				menu.setParentMenu(pk);
			}
			
		}else{
			menu = menuService.getMenuInfo(pk);
			
			if(StringUtils.equals(menu.getParentMenu(), "0")){
				menu.setParentMenu("");
			}
			
		}
		
		if(!StringUtils.equals(menu.getParentMenu(), "0")){//父菜单为空
			MenuInfo menuInfo = menuService.getMenuInfo(menu.getParentMenu());
			if(menuInfo != null){
				parentMenuName = menuInfo.getMenuName();
			}
		}
		
		model.put("parentMenuName", parentMenuName);
		model.put("menu", menu);
		model.put("flag", flag);
		return "menu/detail";
	}

	/**
	 * 新增
	 * @param menu
	 * @return
	 */
	@Resource(name="seqDao")
	private SeqDao seqDao;
	private final static Integer NEWMENUID_DIGITS=5;
	
	@RequestMapping("add")
	@ResponseBody
	public String[] add(MenuInfo menu) {
		if("".equals(menu.getParentMenu())){
			menu.setParentMenu(null);
		}
		String newMenuId=String.valueOf(seqDao.getAndIncreaseSeq("menuId"));
		for(;newMenuId.length()<NEWMENUID_DIGITS;){
			newMenuId="0"+newMenuId;
		}
		menu.setMenuId(newMenuId);
		
		menuService.addMenuInfo(menu);
		return new String[] { "ok", "添加成功！" };
	}

	/**
	 * 删除
	 * @param menu
	 * @return
	 */
	@RequestMapping("remove")
	@ResponseBody
	public String[] remove(String menuId) {
		if(StringUtils.isBlank(menuId)){
			return new String[] { "fail", "删除失败！菜单编号为空！" };
		}
		menuService.removeMenuInfo(menuId);
		return new String[] { "ok", "删除成功！" };
	}

	/**
	 * 删除菜单前判断是否存在子菜单
	 * @param menuId
	 * @return
	 */
	@RequestMapping("checkMenu")
	@ResponseBody
	public String checkMenu(String menuId) {
		return menuService.checkSubMenu(menuId);
	}

	/**
	 * 修改
	 * @param menu
	 * @return
	 */
	@RequestMapping("modify")
	@ResponseBody
	public String[] modify(MenuInfo menu) {
		if("".equals(menu.getParentMenu())){
			menu.setParentMenu(null);
		}
		menuService.modifyMenuInfo(menu);
		return new String[] { "ok", "更新成功！" };
	}
}
